<template>
  <div class="permission">
    <el-card>
      <el-button
        type="primary"
        size="small"
        class="el-icon-edit"
        @click="openAdd('1', '0')"
        >添加菜单</el-button
      >
      <el-table :data="tableData" style="width: 100%" row-key="id">
        <el-table-column prop="name" label="菜单名称" width="180">
        </el-table-column>
        <el-table-column prop="code" label="权限标识" width="180">
        </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="openAdd('2', row.id)"
              v-if="row.pid === '0'"
              >添加权限点</el-button
            >
            <el-button type="text">查看api权限</el-button>
            <el-button type="text" @click="openDialog(row)">查看</el-button>
            <el-button type="text" @click="delSysPermission(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <adedDialog ref="aded" :showAded.sync="showAded"></adedDialog>
  </div>
</template>


<script>
import { getSysPermissionApi, delSysPermissionApi } from "@/api/permission";
export default {
  name: "permission",
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      // 编辑弹框
      showAded: false,
    };
  },
  components: {
    adedDialog: () => import("./component/adEdDialog.vue"),
  },
  methods: {
    // 删除权限点
    async delSysPermission(id) {
      try {
        // 询问是否删除
        await this.$confirm("是否删除当前权限");
        // 删除数据
        await delSysPermissionApi(id);
        // 提示
        this.$message.success("删除成功");
        // 更新数据
        this.getSysPermission();
      } catch (err) {
        this.$message.error(err.message || err);
      }
    },
    // 打开添加权限点弹框
    openAdd(type, pid) {
      this.showAded = true;
      this.$refs.aded.setProperty(type, pid);
    },
    // 打开查看弹框
    openDialog(row) {
      this.showAded = true;
      this.$refs.aded.querySysPermission(row.id);
    },
    // 数据转树形结构
    tranTree(arr, str) {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.tranTree(arr, item.id);
          return true;
        }
        return false;
      });
    },
    // 获取全部权限
    async getSysPermission() {
      const res = await getSysPermissionApi();
      this.tableData = this.tranTree(res, "0");
    },
  },
  created() {
    this.getSysPermission();
  },
};
</script>

<style lang="scss" scoped>
.permission {
  padding: 20px;
}
</style>